package test

import java.io.File

import org.apache.commons.io.FileUtils

import scala.collection.JavaConverters._

object test02 {


  /**
    * 生成帮助文档目录
    * @param args
    */
  def main(args: Array[String]): Unit = {

    getMulu

    getResult

    getLeft




  }

  def getLeft = {
    val f = FileUtils.readLines(new File("D:\\转录组软件官网\\结果.txt")).asScala


    var se = 0
    var to = 0

    val w = f.map(_.split("\t")).map{x=>
      if(x.last == "section"){
        se += 1
        s"""
           |</ul></li>
           |<li class="chapter" data-level="1.1.${se}"  data-name="${x.head}">
           |            <b>${x.head}</b>
           |            <ul class="articles">
        """.stripMargin
      }else if(x.last == "label"){
        to += 1
        s"""
           | <li class="chapter" data-level="1.1.${se}.${to}"  data-name="${x.head}">
           |              <a style='color:#52AB6C;'>${x.head}</a>
           |            </li>
         """.stripMargin
      }else{
        to += 1
        val id = "1_1_" + se + "_" + to
        s"""
           | <li class="chapter" data-level="1.1.${se}.${to}"  data-name="${x.head}" title="${x(1)}">
           |              <a onclick="openHelp('${x(2)}','${id}')" href="#"  id="${id}">${x.head}</a>
           |            </li>
         """.stripMargin
      }
    }.mkString

    FileUtils.writeStringToFile(new File("D:\\转录组软件官网\\left.txt"),w)
  }

  def getResult = {
    val a = FileUtils.readLines(new File("D:\\转录组软件官网\\目录.txt")).asScala

    val path = "D:\\转录组软件官网\\转录组软件帮助文档\\tools\\"
    a.foreach { x =>

      val s = x.split("\t")

      if (s.last == "tool") {

        val a = FileUtils.readFileToString(new File(path + s.head))

        val line = a.slice(a.indexOf("name"), a.indexOf("<command>"))

        val i1 = line.indexOf("\"") + 1
        val i2 = line.drop(i1).indexOf("\"") + i1

        val name = line.slice(i1, i2)

        val des = if (line.indexOf("description") != -1) {
          val last = line.drop(line.indexOf("description") + 1)

          val d1 = last.indexOf("\"") + 1
          val d2 = last.drop(d1).indexOf("\"") + d1
          last.slice(d1, d2)
        }

        FileUtils.writeStringToFile(new File("D:\\转录组软件官网\\结果.txt"), name + "\t" + des + "\t" + s.head.replaceAll(".xml", ".html") + "\n", true)
      } else {
        FileUtils.writeStringToFile(new File("D:\\转录组软件官网\\结果.txt"), x + "\n", true)
      }

    }

  }


  def getMulu = {
    val a = FileUtils.readLines(new File("D:\\转录组软件官网\\转录组软件帮助文档\\configs/tool_conf.xml")).asScala

    a.filter(x => x.contains("<tool") || x.contains("<label") || x.contains("<section")).foreach { x =>

      val i1 = x.indexOf("\"")
      val i2 = x.drop(i1 + 1).indexOf("\"") + i1

      val t = x match {
        case i if i.contains("<tool") => "tool"
        case i if i.contains("<label") => "label"
        case i if i.contains("<section") => "section"
      }

      if (i1 != -1) {
        println(x.slice(i1 + 1, i2 + 1))

        FileUtils.writeStringToFile(new File("D:\\转录组软件官网\\目录.txt"), x.slice(i1 + 1, i2 + 1) + "\t" + t + "\n", true)

      }

    }
  }
}
